import { defineStore } from 'pinia';
import { ref, computed } from 'vue';
import { commandService } from '../services/commandService';

interface Command {
  id: string;
  title: string;
}

export const useCommandPaletteStore = defineStore('commandPalette', () => {
  const isOpen = ref(false);
  const searchQuery = ref('');
  const allCommands = ref<Command[]>([]);

  const filteredCommands = computed(() => {
    if (!searchQuery.value) {
      return allCommands.value;
    }
    return allCommands.value.filter(command =>
      command.title.toLowerCase().includes(searchQuery.value.toLowerCase())
    );
  });

  function open() {
    // 每次打开时都刷新命令列表
    allCommands.value = commandService.getCommands();
    isOpen.value = true;
  }

  function close() {
    isOpen.value = false;
    searchQuery.value = '';
  }

  function execute(commandId: string) {
    commandService.execute(commandId);
    close();
  }

  return {
    isOpen,
    searchQuery,
    filteredCommands,
    open,
    close,
    execute,
  };
});